package com.example.bootdemo5.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * redis 配置文件
 */
@Configuration
public class RedisConfig {


    /**
     *
     * 为了更改序列化的方式 把二进制的序列化方式改成json方式
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String, Serializable> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        // 设置key序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer()); // string数据类型的key序列化
        redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // hash数据类型的key序列化
        // 设置value序列化方式
        redisTemplate.setValueSerializer(new GenericFastJsonRedisSerializer()); // string数据类型的value序列化
        redisTemplate.setHashValueSerializer(new GenericFastJsonRedisSerializer()); // hash数据类型的value序列化
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return  redisTemplate;
    }
}
